package com.easyweb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.easyweb.cache.StartUpListener;
import com.easyweb.common.Constants;
import com.easyweb.common.PrimaryKeyGenerator;
import com.easyweb.common.PrintUtils;
import com.easyweb.model.Article;
import com.easyweb.model.NavBar;
import com.easyweb.model.ResultMsg;
import com.easyweb.model.Users;
import com.easyweb.service.ArticleService;
import com.easyweb.service.NavBarService;

@Controller
@RequestMapping("/navbar")
public class NavBarController {
	@Autowired
	private NavBarService navBarService;
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(value="/merge",method=RequestMethod.POST)
	public void addNavBar(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		if(StringUtils.isEmpty(title)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("title.notnull")));
			return;
		}
		String url = request.getParameter("url");
		if(StringUtils.isEmpty(url)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("url.notnull")));
			return;
		}
		String icon = request.getParameter("icon");
		String showOnIndex = request.getParameter("showOnIndex");
		if(StringUtils.isEmpty(showOnIndex)||(!"1".equals(showOnIndex)&&!"2".equals(showOnIndex))){
			showOnIndex = "2";
		}
		NavBar navBar = new NavBar();
		if(StringUtils.isEmpty(id)){
			//新增
			navBar.setId(PrimaryKeyGenerator.generatePrimaryKey());
			navBar.setSortNum(navBarService.getNavBarCount(new HashMap<String, Object>())+1);
//			int sortNum = navBarService.selectMaxSortNum();
//			navBar.setSortNum(sortNum+1);
		}else{
			navBar.setId(id);
		}
		navBar.setTitle(title);
		navBar.setUrl(url);
		navBar.setIcon(icon);
		navBar.setShowOnIndex(Integer.parseInt(showOnIndex));
		int num = 0;
		if(StringUtils.isEmpty(id)){
			num = navBarService.insertNavBar(navBar);
		}else{
			num = navBarService.updateNavBar(navBar);
		}
		if(num > 0){
			StartUpListener.refreshNavBarCache(navBarService, navBar.getId());
			PrintUtils.printMsg(response, new ResultMsg());
		}else{
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,null));
		}
	}
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public void getNavBars(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String pageSize = request.getParameter("pageSize");
		String pageStart = request.getParameter("pageStart");
		String showOnIndex = request.getParameter("showOnIndex");
		Map<String, Object> map = new HashMap<String, Object>();
		if(!StringUtils.isEmpty(pageSize)){
			map.put("pageSize", Integer.parseInt(pageSize));
		}
		if(!StringUtils.isEmpty(pageStart)){
			map.put("start",  Integer.parseInt(pageSize)*(Integer.parseInt(pageStart)-1));
		}
		if(!StringUtils.isEmpty(showOnIndex)){
			map.put("showOnIndex",  showOnIndex);
		}
		PrintUtils.printMsg(response, navBarService.getNavBars(map));
	}
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public void getNavBarById(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isEmpty(id)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("id.notnull")));
			return;
		}
		map.put("id", id);
		PrintUtils.printMsg(response, navBarService.getNavBarById(map));
	}
	
	@RequestMapping(value="/del",method=RequestMethod.GET)
	public void delNavBarById(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isEmpty(id)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("id.notnull")));
			return;
		}
		//先查询此版块下有无文章
		map.put("navbar", id);
		List<Article> list = articleService.getArticles(map);
		if(list==null||list.size()==0){
			map.put("id", id);
			int num = 0;
			num = navBarService.deleteNavBarById(map);
			if(num > 0){
				StartUpListener.refreshNavBarCache(navBarService, id);
				PrintUtils.printMsg(response, new ResultMsg());
			}else{
				PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,null));
			}
		}else{
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,StartUpListener.LANGUAGE.getProperty("navbar.hasarticle")));
		}
		
	}
}
